﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using QS.Logic.Objects;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace QS.Game.Logic.Objects
{
    class QBulletManager
    {
        private static QBulletManager instance;

        private List<QLaserBullet> bullets;
        private ContentManager contentManager;
        private GraphicsDevice graphicsDevice;

        public static QBulletManager getInstance()
        {
            if (instance == null)
                instance = new QBulletManager();
            return instance;
        }

        public void Load(GraphicsDevice graphicsDevice, ContentManager contentManager)
        {
            this.graphicsDevice = graphicsDevice;
            this.contentManager = contentManager;
        }

        private QBulletManager()
        {
            bullets = new List<QLaserBullet>();
        }

        public void createBullet(Vector3 postition, Matrix orientation)
        {
            QLaserBullet newBullet = QObjectFactory.GetInstance(typeof(QLaserBullet), graphicsDevice, contentManager, null) as QLaserBullet;
            newBullet.Orientation = orientation;
            newBullet.Position = postition + Vector3.Transform(new Vector3(0, 0, -0.5f), orientation);
            newBullet.Speed = Vector3.Transform(Vector3.Forward, orientation);
            this.bullets.Add(newBullet);
        }

        public void Update(GameTime time)
        {
            List<QLaserBullet> toRemove = new List<QLaserBullet>();
            foreach (QLaserBullet bullet in bullets)
            {
                bullet.Update(time);
                if (bullet.IsDead)
                    toRemove.Add(bullet);
            }
            foreach (QLaserBullet bullet in toRemove)
                bullets.Remove(bullet);
        }

        public void Draw(GraphicsDevice device, QCamera camera, SpriteBatch spriteBatch)
        {
            foreach (QLaserBullet bullet in bullets)
                bullet.Draw(device, camera, spriteBatch);
        }
    }
}
